package com.sjmsks.Jpanels.work;

import com.sjmsks.Jpanels.work.shape.*;
import com.sjmsks.Jpanels.work.utils.YamlUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

public class MyFrame extends JFrame {


	public static AbstractShape[] shapeList = new AbstractShape[5000];

	public static int currentIndex = 0;

	public static int currentChoice = 0;

	private static final Stroke STROKE_THIN = new BasicStroke(LineThickness.THIN);
	private static final Stroke STROKE_NORMAL = new BasicStroke(LineThickness.NORMAL);
	private static final Stroke STROKE_THICK = new BasicStroke(LineThickness.THICK);
	static Stroke stroke = STROKE_NORMAL;

	static Color color = new Color(0, 0, 0);
	JButton circle = new JButton("圆形");
	JButton triangle = new JButton("三角形");
	JButton rectangle = new JButton("矩形");

	JButton thin = new JButton("细");

	JButton normal = new JButton("正常");

	JButton thick = new JButton("粗");

	JButton clear = new JButton("清除");

	JButton red = new JButton("红色");

	JButton green = new JButton("绿色");

	JButton blue = new JButton("蓝色");

	static class DrawPanel extends JPanel {

		DrawPanel() {
			// 设置光标类型，为十字形
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
			// 设置背景颜色
			this.setBackground(Color.white);
			// 设置鼠标监听
			this.addMouseListener(new MouseAction());
			this.addMouseMotionListener(new MouseMotion());

		}

		public ShapeIterator createIterator() {
			return new ShapeListIterator(shapeList);
		}

		// 重写paintComponent方法，使得画板每次刷新时可将之前的所有图形重新画出来。
		@Override
		public void paintComponent(Graphics g) {
			if (currentIndex == 0) {
				return;
			}
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g; // 定义画板
			ShapeIterator iterator = createIterator();
			while (iterator.hasNext()) {
				AbstractShape shape = iterator.next();
				draw(g2d, shape);
			}
		}

		void draw(Graphics2D g2d, AbstractShape abstractShape) {
			// 将画笔传入到各个子类中，用来完成各自的绘图
			abstractShape.draw(g2d);
		}

		void createNewGraphics() throws UnSupportShapeException, CloneNotSupportedException {

			switch (currentChoice) {
				case 0:
					shapeList[currentIndex]
							= ShapeFactory.getInstance().getShape("circle", color, stroke);
					break;
				case 1:
					shapeList[currentIndex]
							= ShapeFactory.getInstance().getShape("triangle", color, stroke);
					break;
				case 2:
					shapeList[currentIndex]
							= ShapeFactory.getInstance().getShape("rectangle", color, stroke);
					break;
				default:
			}
		}


		/**
		 * 新建一个画图基本单元对象的程序段
		 */

		// 鼠标事件mouseAction类，继承了MouseAdapter，用来完成鼠标相应事件操作
		class MouseAction extends MouseAdapter {
			@Override
			public void mousePressed(MouseEvent e) {
				shapeList[currentIndex].x1 = shapeList[currentIndex].x2 = e.getX();
				shapeList[currentIndex].y1 = shapeList[currentIndex].y2 = e.getY();
				System.out.println("mousePressed,x,y:" + e.getX() + e.getY());
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				shapeList[currentIndex].x2 = e.getX();
				shapeList[currentIndex].y2 = e.getY();
				repaint();
				currentIndex++;
				try {
					createNewGraphics();
				} catch (UnSupportShapeException | CloneNotSupportedException ex) {
					throw new RuntimeException(ex);
				}
				System.out.println("mouseReleased,x,y:" + e.getX() + e.getY());

			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}
		}

		/**
		 * 鼠标事件mouseMotion类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的响应操作
		 */
		class MouseMotion extends MouseMotionAdapter {
			@Override
			public void mouseDragged(MouseEvent e) {
				shapeList[currentIndex].x2 = e.getX();
				shapeList[currentIndex].y2 = e.getY();
				repaint();
			}


			@Override
			public void mouseMoved(MouseEvent e) {
			}
		}

	}


	MyFrame(String title) throws UnSupportShapeException, CloneNotSupportedException, IOException {


		// 封装自己的窗口
		super(title);
		JToolBar jToolBar = new JToolBar();

		jToolBar.setOrientation(SwingConstants.VERTICAL);

		Map<String, Object> config = YamlUtils.YamlToJsonConverter();
		Map<String, Boolean> colorConfig = (Map<String, Boolean>) config.get("color");
		Map<String, Boolean> shapeConfig = (Map<String, Boolean>) config.get("shape");

		if (shapeConfig.get("circle")) {
			jToolBar.add(circle);
		} else {
			currentChoice = 1;
		}

		if (shapeConfig.get("triangle")) {
			jToolBar.add(triangle);
		} else {
			currentChoice = 2;
		}

		if (shapeConfig.get("rectangle")) {
			jToolBar.add(rectangle);
		}

		jToolBar.add(thin);
		jToolBar.add(normal);
		jToolBar.add(thick);

		if (colorConfig.get("red")) {
			jToolBar.add(red);
		}

		if (colorConfig.get("green")) {
			jToolBar.add(green);
		}

		if (colorConfig.get("blue")) {
			jToolBar.add(blue);
		}

		jToolBar.add(clear);

		add(jToolBar, BorderLayout.WEST);

		DrawPanel drawPanel = new DrawPanel();

		add(drawPanel, BorderLayout.CENTER);

		drawPanel.setLayout(new BoxLayout(drawPanel, BoxLayout.Y_AXIS));

		drawPanel.createNewGraphics();

		clear.addActionListener(e -> {
			currentIndex = 0;
			Arrays.fill(shapeList, null);
			try {
				drawPanel.createNewGraphics();
			} catch (UnSupportShapeException | CloneNotSupportedException ex) {
				throw new RuntimeException(ex);
			}
			repaint();
		});

		circle.addActionListener(e -> {
			currentChoice = 0;
			try {
				drawPanel.createNewGraphics();
			} catch (UnSupportShapeException | CloneNotSupportedException ex) {
				throw new RuntimeException(ex);
			}
		});

		triangle.addActionListener(e -> {
			currentChoice = 1;
			try {
				drawPanel.createNewGraphics();
			} catch (UnSupportShapeException | CloneNotSupportedException ex) {
				throw new RuntimeException(ex);
			}
		});

		rectangle.addActionListener(e -> {
			currentChoice = 2;
			try {
				drawPanel.createNewGraphics();
			} catch (UnSupportShapeException | CloneNotSupportedException ex) {
				throw new RuntimeException(ex);
			}
		});

		red.addActionListener(e -> {
			System.out.println("切换颜色为红色");
			color = Color.red;
			shapeList[currentIndex].setColor(color);
		});

		green.addActionListener(e -> {
			System.out.println("切换颜色为绿色");
			color = Color.green;
			shapeList[currentIndex].setColor(color);
		});

		blue.addActionListener(e -> {
			System.out.println("切换颜色为蓝色");
			color = Color.blue;
			shapeList[currentIndex].setColor(color);
		});


		thin.addActionListener(e -> {
			System.out.println("切换线条宽度为细");
			stroke = STROKE_THIN;
			shapeList[currentIndex].setStroke(stroke);
		});
		normal.addActionListener(e -> {
			System.out.println("切换线条宽度为正常");
			stroke = STROKE_NORMAL;
			shapeList[currentIndex].setStroke(stroke);
		});
		thick.addActionListener(e -> {
			System.out.println("切换线条宽度为粗");
			stroke = STROKE_THICK;
			shapeList[currentIndex].setStroke(stroke);
		});
	}

}